package com.mdd.common.utils.phone;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dyplsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dyplsapi20170525.models.BindAxbRequest;
import com.aliyun.sdk.service.dyplsapi20170525.models.BindAxbResponse;
import com.aliyun.sdk.service.dyplsapi20170525.models.BindAxbResponseBody;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Slf4j
@Component
public class AliPhoneUtil {
	private static String aliAccessKeyId;

	@Value("${ali.access.key.id:LTAI5t9eKY8eqJmfSVr18KLR}")
	public void setAliAccessKeyId(String aliAccessKeyId) {
		this.aliAccessKeyId = aliAccessKeyId;
	}

	private static String aliAccessKeySecret;

	@Value("${ali.access.key.secret:KI4epPA3igg4Jbw3vAbrWyflKH0BK6}")
	public void setAliAccessKeySecret(String aliAccessKeySecret) {
		this.aliAccessKeySecret = aliAccessKeySecret;
	}

	private static String aliAccessKeyRegion;

	@Value("${ali.access.key.region:cn-shenzhen}")
	public void setAliAccessKeyRegion(String aliAccessKeyRegion) {
		this.aliAccessKeyRegion = aliAccessKeyRegion;
	}

	private static String aliAccessKeyPhoneKey;

	@Value("${ali.access.key.phoneKey:FC100000178738158}")
	public void setAliAccessKeyPhoneKey(String aliAccessKeyPhoneKey) {
		this.aliAccessKeyPhoneKey = aliAccessKeyPhoneKey;
	}

	public static String getVirtualNumbers(String staffPhoneNumber, String userPhoneNumber) throws ExecutionException, InterruptedException {

		StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
				.accessKeyId(aliAccessKeyId)//accessKey
				.accessKeySecret(aliAccessKeySecret)//accessKeySecret
				.build());

		AsyncClient client = AsyncClient.builder()
				.region(aliAccessKeyRegion) // Region ID
				.credentialsProvider(provider)
				.overrideConfiguration(
						ClientOverrideConfiguration.create()
								.setEndpointOverride("dyplsapi.aliyuncs.com")
				)
				.build();
		Calendar now = Calendar.getInstance();
		now.setTime(new Date());
		now.add(Calendar.MINUTE, 2);
		String nowFormatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now.getTime());

		BindAxbRequest bindAxbRequest = BindAxbRequest.builder()
				.phoneNoA(staffPhoneNumber)//师傅号码（拨号者）
				.phoneNoB(userPhoneNumber)//客户号码（被拨号者）
				.expiration(nowFormatDate)//过期时间
				.poolKey(aliAccessKeyPhoneKey)//号码池KEY
				.build();
		log.info("getVirtualNumbers中的2个号码分别是： {}---------{}", staffPhoneNumber, userPhoneNumber);
		CompletableFuture<BindAxbResponse> response = client.bindAxb(bindAxbRequest);
		//获取到虚拟手机号码
		try {
			BindAxbResponseBody.SecretBindDTO secretBindDTO = response.get().getBody().getSecretBindDTO();
			//secretBindDTO对象不为Null则说明号码绑定成功，如果获取为Null则说明号码池电话都被分配完毕了。
			if (secretBindDTO != null) {
				//获取到虚拟号码，通过此号码进行加密，前端拨打此号码即可正确跳转到客户真实号码
				return secretBindDTO.getSecretNo();
			}
		} finally {
			client.close();
		}
		return null;
	}
}
